import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2025-04-27
 * Time: 20:15
 */
public class demo6 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(10);

        ExecutorService executor = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 10; i++) {
            int id = i;
            executor.submit(() -> {
                System.out.println("子线程开始执行：" + id);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程结束执行：" + id);
                latch.countDown();
            });
        }

        //这个方法阻塞等待所有的任务结束
        //此处的 a => all
        latch.await();
        System.out.println("所有任务执行完毕");
        executor.shutdown();
    }
}
